package com.snoob.lixing.util;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 将Java对象转换成json对象和xml文档，同样也可以将json、xml转换成Java对象
 *
 * @author EditPlus
 */
public class JacksonUtil {

    private ObjectMapper objectMapper = null;

    public JacksonUtil() {
        objectMapper = new ObjectMapper();
    }

    public static void main(String[] args) {
//		File file = new File("src/config/systree.json");
//		System.out.println(file.getAbsolutePath());
//		System.out.println(new JacksonUtil().returnObj(file,List.class));

//		File file = new File(JqueryController.class.getResource("/config/systree.json").getFile().toString());
//		System.out.println(new JacksonUtil().returnObj(file,List.class));
    }

    /**
     * 将指定目录下的Json数据写到指定的Java Bean,Map中
     */
    public <T> T returnObj(File file, Class<T> clazz) {
        try {
            return (T) objectMapper.readValue(file, clazz);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    //File file = new File("src/config/jackson_01.json");
    //System.out.println(new JacksonUtil().returnObj(file,ZTree.class));
    //System.out.println(new JacksonUtil().returnObj(file,Map.class));
    //File file = new File("src/config/jackson_02.json");
    //System.out.println(new JacksonUtil().returnObj(file,Map.class));
    //File file = new File("src/config/jackson_03.json");
    //System.out.println(new JacksonUtil().returnObj(file,List.class));

    /**
     * 将Java Bean,Map中的数据以Json格式存放到本地目录
     */
    public void returnJson(Object obj, File file) {
        try {
            objectMapper.writeValue(file, obj);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //ZTree tree = new ZTree();
    //tree.setId("31011225");
    //tree.setName("sa");
    //tree.setPid("310112");
    //tree.setTarget("_self");
    //tree.setUrl("www.baidu.com/");
    //new JacksonUtil().returnJson(tree,new File("src/config/jackson.json"));
    //Map<String,Object> map = new HashMap<String,Object>();
    //map.put("id", "31011224");
    //map.put("name", "sa");
    //map.put("pid", "310112");
    //map.put("target", "_self");
    //map.put("url", "www.baidu.com/");
    //new JacksonUtil().returnJson(map,new File("src/config/jackson.json"));

    //使用JsonGenerator来转换JSON，那么你必须创建一个ObjectMapper
    //但是你用ObjectMapper来转换JSON，则不需要JSONGenerator

    /**
     * Java Bean,Map,List转化成Json格式字符串
     */
    public String returnJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Json转换成Java Bean,Map,List
     */
    public <T> T readValue(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
